package my.sorter.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import my.sorter.bean.MyPageInfo;
import my.sorter.bean.filter.DetailFilter;
import my.sorter.dao.model.Detail;
import my.sorter.service.SortService;
import my.sorter.translator.out.MyMessage;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author zyf10
 */
@Controller
public class DetailController {
    @Autowired
    SortService sortService;

    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/detail/create")
    @ResponseBody
    public Object createDetail(Detail detail) {
        Boolean flag = sortService.createDetail(detail);
        return flag ? MyMessage.successMessage().addMessage("在病情库创建新病情成功") : MyMessage.failMessage().addMessage("在病情库创建新病情失败");
    }

    /**
     * 删除病情
     * @param detailIds
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/detail/delete")
    @ResponseBody
    public Object deleteDetail(String detailIds) {
        JSONArray idsArr = JSONArray.parseArray(detailIds);
        List<String> ids = new ArrayList<>();
        for (Object id : idsArr) {
            ids.add(id.toString());
        }
        Boolean flag = sortService.deleteDetail(ids);
        return flag ? MyMessage.successMessage().addMessage("在病情库删除病情成功") : MyMessage.failMessage().addMessage("在病情库删除病情失败");
    }

    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/detail/update")
    @ResponseBody
    public Object updateDetail(Detail detail) {
        Boolean flag = sortService.updateDetail(detail);
        return flag ? MyMessage.successMessage().addMessage("在病情库更新病情成功") : MyMessage.failMessage().addMessage("在病情库更新病情失败");
    }

    /**
     * 查询病情
     *
     * @param filter
     * @param myPageInfo
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @GetMapping("/detail/get")
    @ResponseBody
    public Object getDetails(DetailFilter filter, MyPageInfo myPageInfo) {
        PageHelper.startPage(myPageInfo.getCurrentPage(), myPageInfo.getPageSize());
        List<Detail> details = sortService.getDetails(filter);
        PageInfo pageInfo = new PageInfo(details);
        myPageInfo.setTotal((new Long(pageInfo.getTotal()).intValue()));
        myPageInfo.setTotalPage(pageInfo.getPages());
        return MyMessage.successMessage(myPageInfo).addData(details).addMessage("获取病情库病情列表成功");
    }

    /**
     * 新增病情和删更新病情
     *
     * @param detail
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/detail/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Detail detail) {
        boolean flag = false;
        if (StringUtils.isEmpty(detail.getId())) {
            //新增
            detail.setId(UUID.randomUUID().toString().replace("-", ""));
            flag = sortService.createDetail(detail);
        }
        {
            //更新
            flag = sortService.updateDetail(detail);
        }
        return flag ? MyMessage.successMessage("操作成功") : MyMessage.failMessage("操作成功");
    }
}
